/**
 * Copyright 2011 Adrian Witas
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.abstractmeta.commandify.core;

import org.abstractmeta.commandify.Commandify;
import org.abstractmeta.commandify.OwnerSelfCommandifyAccessor;
import org.abstractmeta.commandify.command.Commands;
import org.abstractmeta.commandify.core.command.OwnerSelfCommandifyAccessorImpl;
import org.abstractmeta.reflectify.Accessor;

/**
 * Represents AbstractCommandify
 *
 * @author Adrian Witas
 */
public class AbstractCommandify<T> implements Commandify<T> {


    protected final Class<T> commandifyType;
    private final Commands<T> commands;


    @SuppressWarnings("unchecked")
    public AbstractCommandify(Class commandifyType) {
        this(commandifyType, new CommandsImpl<T>(commandifyType));
    }


    @SuppressWarnings("unchecked")
    public AbstractCommandify(Class commandifyType, Commands<T> commands) {
        this.commandifyType = commandifyType;
        this.commands = commands;
    }


    @Override
    public Class<T> getCommandifyType() {
        return commandifyType;
    }

    public Commands<T> getCommands() {
        return commands;
    }

    @Override
    @SuppressWarnings("unchecked")
    public OwnerSelfCommandifyAccessor<T, Object> getCommandifyAccessor(Object source) {
        Accessor accessor = commands.getAccessor(source);
        return new OwnerSelfCommandifyAccessorImpl<T, Object>(source, this, accessor);
    }


}

